var indexController = function controller ($scope, $rootScope, $http, $log, $location, $interval, $state, $timeout) {
    //每20分钟与后台交互，确保后台程序会话存在
    $rootScope.timer = $interval(function () {
        getLoginData();
        $scope.curUser = getSessionJson('curUserObj');
    }, 1200000);
    getLoginData();
    $scope.curUser = getSessionJson('curUserObj');

    $scope.toUpdatePwd = function () {
        $state.go("changePwd", {
            "adminUserId": $scope.curUser.id
        });
    };
    $scope.menuList = [];

    $scope.notifyCount = {};

    $scope.order = 'sortOrder';

    $scope.getMenuList = function () {
        $http({
            method: 'GET',
            url: baseServices_restfulUrl + 'admin/adminmenu/listAdminMenusForUser.json?userId=' + $scope.curUser.id
        }).success(function (data, header, config, status) {
            $scope.menuList = data.data;
            // console.log('左侧菜单---->>');
            // console.log($scope.menuList);
        }).error(function (data, header, config, status) {
            console.log(data);
        });

    };

    $scope.getMenuList();

    $scope.getNotifyCount = function () {
        $http({
            method: 'GET',
            url: baseServices_restfulUrl + 'admin/adminNotify/getNotifyCount.json'
        }).success(function (data, header, config, status) {
            $scope.notifyCount = data.data;
        }).error(function (data, header, config, status) {
            console.log(data);
        });

    };

    $scope.getNotifyCount();

    // 每5分钟刷新一次消息提醒
    $rootScope.notifyCountTimer = $interval(function () {
        $scope.getNotifyCount();
        console.log("doNotifyCountTimer");
    }, 300000); // 300000-每5分钟，60000-每分钟

    //左侧树菜单默认展开并选中地址栏中的路由链接
    $timeout(function () {
        var hash = window.location.hash;
        var tempNode = '';

        for (var i = 0; i < $scope.menuList.length; i++) {
            if (!isEmpty($scope.menuList[i].linkUrl)) {
                if (hash.indexOf($scope.menuList[i].linkUrl) !== -1) {
                    $scope.menuList[i].isactive = "active";
                    break;
                }
            }
            tempNode = $scope.menuList[i].children;
            if (!isEmpty(tempNode)) {
                for (var j = 0; j < tempNode.length; j++) {
                    if (hash.indexOf(tempNode[j].linkUrl) !== -1) {
                        tempNode[j].isactive = "active";
                        $scope.menuList[i].isactive = "active";
                        break;
                    }
                }
            }
        }

    }, 500);

    $scope.logout = function () {
        if (!confirm("确定退出系统吗？")) {
            return;
        }
        $http({
            method: 'POST',
            url: baseServices_restfulUrl + 'admin/adminuser/logout.json',
            params: {},
            headers: {
                "Content-Type": "application/x-www-form-urlencoded;charset=utf-8"
            }
        }).success(function (data, header, config, status) {
            location.href = webStaticPath + "login.html";
            removeSession('curUserObj');

        }).error(function (data, header, config, status) {
            location.href = webStaticPath + "login.html";
        });
    };

    //当前点击的菜单及父菜单
    $scope.getCurMenu = function (relmenu, pmenu) {
        $rootScope.curMenu = relmenu;
        $rootScope.curPMenu = pmenu;
    };

    // 取消，返回
    $scope.close = function () {
        history.back();
    };
};
